package proxy.dynamicProxy.jdk;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class ProxyOne implements InvocationHandler {

    private Person person;

    public ProxyOne(){}

    public ProxyOne(Person person){
        this.person = person;
    }

    public Object getProxy(){
        System.out.println("动态获得代理类对象");
        return Proxy.newProxyInstance(this.getClass().getClassLoader(),
                                        person.getClass().getInterfaces(),
                                    this);
    }


    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {

        System.out.println("即将调用目标方法");
        Object result = method.invoke(person, args);
        System.out.println("目标方法已调用完毕");
        return result;
    }
}
